const express = require('express');
var router = express.Router();
const fs = require('fs');
const {multer} = require('../../model/tools')
const FocusModel = require('../../model/focusModel')


router.get("/",(req,res)=>{
    //获取轮播图数据
    FocusModel.find({},(err,data)=>{
    if(err){
      console.log(err);
      return;
    }
    res.render("admin/focus/index.html",{
      list:data
    });
  })

 
})

//渲染增加轮播图页面
router.get("/add",(req,res)=>{
   res.render("admin/focus/add.html")
})

//增加轮播图
router.post("/doAdd",multer().single('focus_img'),(req,res)=>{
    var focus_img = req.file ? req.file.path.split("\\").slice(1).join("/") : '';
    var result = new FocusModel(Object.assign(req.body,{focus_img}))
    result.save((err)=>{
        if(err){
            console.log(err);
            return;
        }
        res.render("admin/public/success.html",{
            redirectUrl:`/${req.app.locals.adminPath}/focus`,
            message:"添加成功"
        });
    })
})

//删除轮播图
router.get("/delete",async (req,res)=>{
    //获取要删除的轮播图id
    let {id} = req.query;
    var resultList = await FocusModel.find({_id:id});
    var deResult = await FocusModel.deleteOne({_id:id});
    if(deResult.deletedCount == 1 ){//不仅删除数据还要删除上传的图片
        if(resultList[0].focus_img){
            fs.unlink("static/" + resultList[0].focus_img,(err)=>{
                if(err){
                    console.log(err);
                    return;
                }
            })
        }
    }
    res.render("admin/public/success.html",{
        redirectUrl:`/${req.app.locals.adminPath}/focus`,
        message:"删除成功"
    })
  
})

//回显轮播图
router.get("/edit",(req,res)=>{
    //获取要修改的轮播图id
    let {id} = req.query;
    FocusModel.find({_id:id},(err,data)=>{
        if(err){
          console.log(err);
          return;
        }
        if(data.length>0){
          res.render("admin/focus/edit.html",{
            list:data[0]
          });
        }else{
          res.redirect(`/${req.app.locals.adminPath}/focus`);
        }
     
      })
  
  })

//修改轮播图逻辑
router.post("/doEdit",multer().single('focus_img'),(req,res)=>{
    let {id,type,title,link,sort,status} = req.body;
    if(req.file){//更新了图片
        var focus_img = req.file.path.split("\\").slice(1).join("/");
        FocusModel.updateOne({_id:id},{
            type,
            title,
            link,
            focus_img,
            sort,
            status
            },(err)=>{
            if(err){
                console.log(err);
                return;
            }
            res.render("admin/public/success.html",{
                redirectUrl:`/${req.app.locals.adminPath}/focus`,
                message:"修改成功"
            });
        })
    }else{//没更新图片
        FocusModel.updateOne({_id:id},{
            type,
            title,
            link,
            sort,
            status
            },(err)=>{
            if(err){
                console.log(err);
                return;
            }
            res.render("admin/public/success.html",{
                redirectUrl:`/${req.app.locals.adminPath}/focus`,
                message:"修改成功"
            });
        })
    }
})
module.exports = router